package com.example.travelsn.homepage;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.travelsn.R;
import com.example.travelsn.cate.CateHome;
import com.example.travelsn.db.DBManager;
import com.example.travelsn.db.Feedback;
import com.example.travelsn.db.Seller;
import com.example.travelsn.db.User;
import com.luluteam.itestlib.apm.test.tool.FragmentProxy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminFragment extends Fragment {
    //布局和数据
    private ListView lvAdmin;
    private DBManager dbManager;
    //按钮控件
    private Button bSelectFeed;
    private Button bManageUser;
    private Button bManageSeller;
    private int selectType ;
    //listView相关
    private List<Map<String, String>> listItem;
    List<Feedback> feedbackList = new ArrayList<>();
    List<User> userlist = new ArrayList<>();
    List<Seller> sellerlist = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentProxy.callonCreate(this);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_admin, container, false);
    }
    @Override
    public void  onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();
        bSelectFeed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectType =1;
                selectFeedback();
        }
        });
        bManageUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectType =2;
                manageUser();
            }
        });
        bManageSeller.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectType =3;
                manageSeller();
            }
        });
        lvAdmin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 通过getAdapter()方法取得MyAdapter对象，再调用getItem(int)返回一个Data对象
                String text =  lvAdmin.getAdapter().getItem(position).toString();
                // 将点击的item里面的字弹出来
                Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
            }
        });
        lvAdmin.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                if(selectType == 1){
                    //反馈处理
                    new AlertDialog.Builder(getContext()).setTitle("已处理？")
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 点击“确认”后的操作
                                    HashMap<String,String> map=(HashMap<String,String>)lvAdmin.getItemAtPosition(position);
                                    dbManager.upFeedBack(map.get("username"),map.get("feedcontent"));
                                    selectFeedback();
                                }
                            }).show();
                }else if(selectType == 2){
                    //用户删除
                    new AlertDialog.Builder(getContext()).setTitle("用户删除？")
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setPositiveButton("确定删除嘛", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 点击“确认”后的操作
                                    HashMap<String,String> map=(HashMap<String,String>)lvAdmin.getItemAtPosition(position);
                                    dbManager.deleteUser(map.get("username"));
                                    Toast.makeText(getActivity(),"用户"+map.get("username")+"删除成功",Toast.LENGTH_SHORT).show();
                                    manageUser();
                                }
                            }).show();
                }else if(selectType == 3){
                    new AlertDialog.Builder(getContext()).setTitle("商家删除？")
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setPositiveButton("确定删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 点击“确认”后的操作
                                    HashMap<String,String> map=(HashMap<String,String>)lvAdmin.getItemAtPosition(position);
                                    dbManager.deleteSeller(map.get("sellername"));
                                    Toast.makeText(getActivity(),"用户"+map.get("sellername")+"删除成功",Toast.LENGTH_SHORT).show();
                                    manageSeller();
                                }
                            }).show();
                }
                return true;
            }
        });



    }
    @Override
    public void onResume() {
        super.onResume();
        FragmentProxy.callonResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        FragmentProxy.callonPause(this);
    }

    private void initview(){
        lvAdmin = (ListView) getActivity().findViewById(R.id.lv_admin);
        bSelectFeed = (Button)getActivity().findViewById(R.id.b_skimfeed);
        bManageUser = (Button)getActivity().findViewById(R.id.b_manageuser);
        bManageSeller =  (Button)getActivity().findViewById(R.id.b_manageseller);
        dbManager = new DBManager(getContext());
        listItem = new ArrayList<Map<String, String>>();
        selectType = 0;
    }
    private void selectFeedback(){
        listItem.removeAll(listItem);
        feedbackList = dbManager.getFeedbacks();
        for (int i = 0; i < feedbackList.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("feedcontent", feedbackList.get(i).getContent());
            map.put("feedstatus", feedbackList.get(i).getStatus());
            map.put("username", feedbackList.get(i).getUserName());
            if(!map.isEmpty() && map !=null){
                listItem.add(map);
            }else System.out.println("map为空");

        }
        if(listItem!=null && !listItem.isEmpty()){
            SimpleAdapter adapter = new SimpleAdapter(getActivity()
                    , listItem
                    , R.layout.listview_item
                    , new String[]{"feedcontent", "feedstatus"}
                    , new int[]{R.id.feed_content, R.id.feed_user});
            lvAdmin.setAdapter(adapter);
        }
    }
    private void manageUser(){
        listItem.removeAll(listItem);
        userlist = dbManager.getUsers();
        for (int i = 0; i < userlist.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("username", userlist.get(i).getUserName());
            map.put("usertype", "普通用户");
            if(!map.isEmpty() && map !=null){
                listItem.add(map);
            }else System.out.println("map为空");

        }
        if(listItem!=null && !listItem.isEmpty()){
            SimpleAdapter adapter = new SimpleAdapter(getActivity()
                    , listItem
                    , R.layout.listview_item
                    , new String[]{"username", "usertype"}
                    , new int[]{R.id.feed_content, R.id.feed_user});
            lvAdmin.setAdapter(adapter);
        }
    }
    private void manageSeller(){
        listItem.removeAll(listItem);
        sellerlist = dbManager.getSeller();
        for (int i = 0; i < sellerlist.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("sellername", sellerlist.get(i).getSellerName());
            map.put("sellertype", "seller");
            if(!map.isEmpty() && map !=null){
                listItem.add(map);
            }else System.out.println("map为空");

        }
        if(listItem!=null && !listItem.isEmpty()){
            SimpleAdapter adapter = new SimpleAdapter(getActivity()
                    , listItem
                    , R.layout.listview_item
                    , new String[]{"sellername", "sellertype"}
                    , new int[]{R.id.feed_content, R.id.feed_user});
            lvAdmin.setAdapter(adapter);
        }
    }
}
